package com.lizhao.win007com.commons.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpUtils {
    public static Map getOddType(){
        Map<String, String> mapOddType = new HashMap<>();
        mapOddType.put("1", "80");
        mapOddType.put("3", "545");
        mapOddType.put("4", "82");
        mapOddType.put("8", "281");
        mapOddType.put("12", "90");
        mapOddType.put("14", "81");
        mapOddType.put("17", "517");
        mapOddType.put("19", "104");
        mapOddType.put("22", "16");
        mapOddType.put("23", "23");
        mapOddType.put("24", "18");
        mapOddType.put("31", "474");
        mapOddType.put("35", "659");
        mapOddType.put("42", "976");
        mapOddType.put("48", "432");
        return mapOddType;
    }
    public static String checkAsianAndBigByPageId(Integer pageid, String content){
        String companyid = "";
        String retContent = "";
        Map mapOddType = getOddType();

        content = content
                    .replaceAll("\\r", "")
                    .replaceAll("\\n", "")
                    .replaceAll("\\t", "")
                    .replaceAll("　", "")
                    .replaceAll("<table.*?id=\"odds\".*?>", "<table id=\"odds\">")
                    .replaceAll("<tr class=\"yellow_bg\".*?>.*?</tr>", "")
                    .replaceAll("<tr.*?bgcolor=.*?>", "<tr>")
                    .replaceAll("<img.*?>", "")
                    .replaceAll("<style.*?>.*?</style>", "")
                    .replaceAll("<script.*?>.*?</script>", "");
        content = content.toLowerCase();

        //正则获取内容
        Pattern pattern1 = Pattern.compile("<table id=\"odds\">(.*?)</table>");
        Matcher matcher1 = pattern1.matcher(content);
        if (matcher1.find()){
            content = matcher1.group(0);
            content = content
                    .replaceAll("<tbody.*?>", "<table border=\"1\">")
                    .replaceAll("</tbody>", "</table>")
                    .replaceAll("<a.*?>.*?</a>", "")
                    .replaceAll("> +<", "><")
                    .replaceAll("<td></td>", "")
                    .replaceAll("<td height=\"[0-9]+\"></td>", "")
                    .replaceAll("<tr><td>盘口.*?</td>.*?</tr>", "")
                    .replaceAll(" >", ">");
        }

        Pattern pattern2 = Pattern.compile("<tr>(.*?)</tr>");
        Matcher matcher2 = pattern2.matcher(content);
        while (matcher2.find()){
//            System.out.println(2222);
            content = matcher2.group(1);

            String begin = "";
            String odds = "";
            String end = "";
            String wholeBegin = "";
            String wholeOdds = "";
            String wholeEnd = "";

            //初水
            Pattern pattern3 = Pattern.compile("<td title=\"\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d\">(.*?)</td>" +
                    "<td title=\"\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d\" goals=\"(.*?)\">.*?</td>" +
                    "<td title=\"\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d\">(.*?)</td>");
            Matcher matcher3 = pattern3.matcher(content);
            if (matcher3.find()){
//                System.out.println(3333);

                //初水、盘
                begin = matcher3.group(1);
                odds = matcher3.group(2);
                end = matcher3.group(3);

                //终水、盘
                Pattern pattern4 = Pattern.compile("<td oddstype=\"wholeodds\">(.*?)</td>" +
                        "<td goals=\"(.*?)\" oddstype=\"wholeodds\">.*?</td>" +
                        "<td oddstype=\"wholeodds\">(.*?)</td>");
                Matcher matcher4 = pattern4.matcher(content);
                if (matcher4.find()){
//                    System.out.println(4444);
                    wholeBegin = matcher4.group(1);
                    wholeOdds = matcher4.group(2);
                    wholeEnd = matcher4.group(3);

                    Pattern pattern5 = Pattern.compile("companyid=\"(.*?)\"");
                    Matcher matcher5 = pattern5.matcher(content);
                    if (matcher5.find()){
//                        System.out.println(5555);
                        companyid = matcher5.group(1);
                        if (mapOddType.get(companyid) != null){
                            content = mapOddType.get(companyid) + "," + begin + "," + odds + "," + end + ","
                                    + wholeBegin + ","+ wholeOdds + "," + wholeEnd + "==";
                            retContent += content;
                        }
                    } else{
                        Pattern pattern6 = Pattern.compile("companyid='(.*?)'");
                        Matcher matcher6 = pattern6.matcher(content);
                        if (matcher6.find()){
//                            System.out.println(6666);
                            companyid = matcher6.group(1);
                            if (mapOddType.get(companyid) != null){
                                content = mapOddType.get(companyid) + "," + begin + "," + odds + "," + end + ","
                                        + wholeBegin + ","+ wholeOdds + "," + wholeEnd + "==";
                                retContent += content;
                            }
                        }
                    }
                }
            }
        }
        retContent = pageid + "==" + retContent.replaceAll("==$", "");
        return retContent;
    }

    public static String checkOddsByPageId(Integer pageid, String content){
        String retContent = "";

        content = content
                .replaceAll("\\r", "")
                .replaceAll("\\n", "")
                .replaceAll("\",\"", "==")
                .replaceAll("var gameDetail=Array\\((.*?)\\);", "");


        //正则获取内容
        Pattern pattern1 = Pattern.compile("game=Array\\(\"(.*?)\"\\);");
        Matcher matcher1 = pattern1.matcher(content);
        if (matcher1.find()){
            content = matcher1.group(1);
        }

        retContent = pageid + "==" + content;
        return retContent;
    }

    //重新生成盘口数据文件
    public static void makeFile(String path, String content){
        try (FileOutputStream fos = new FileOutputStream(path)){
            byte[] data = content.getBytes(StandardCharsets.UTF_8);
            fos.write(data);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}